package com.wtwd.wtwddevice.deviceserver.netty;

import com.wtwd.wtwddevice.deviceserver.netty.code.WTDecoder;
import com.wtwd.wtwddevice.deviceserver.netty.code.WTEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    private static final int READ_IDLE_TIME = 600;
    @Resource
    private BaseChannelHandler channelHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //设置管道
        ChannelPipeline pipeline = socketChannel.pipeline();
        //编解码
        pipeline.addLast("WTDecoder", new WTDecoder());
        pipeline.addLast("WTEncoder", new WTEncoder());
        //300秒没有交互就关闭通道
        pipeline.addLast("readtimeout", new ReadTimeoutHandler(READ_IDLE_TIME));
        // 自定义的handler，处理业务逻辑
        pipeline.addLast(channelHandler);


    }

    public static void main(String[] args) {
        System.out.println("2@0@1@0@1@21AC960_YHC_V01_240423@AC960_V1.0.9_2024-01-12_17-04-22".length());
    }
}
